package com.shan.oss.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.shan.oss.constant.OssConstant;
import com.shan.oss.constant.OssResult;
import com.shan.oss.constant.OssResultConstant;
import com.shan.oss.web.service.AliyunOssService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;

/**
 * Author: shanzheng <br/>
 * Description: 阿里云上传存储对象<br/>
 * Date： 15:53 2017/9/6.<br/>
 * Modified By: <br/>
 */
@Controller
@RequestMapping("/aliyun/oss")
public class AliyunOssController {

    private static Logger logger = LoggerFactory.getLogger(AliyunOssController.class);

    @Autowired
    private AliyunOssService aliyunOssService;

    /**
     * Description: 签名生成
     *
     * @param callback 跨域请求
     * @author: shanzheng
     * @date: 16:43 2017/9/6
     * @return: java.lang.Object
     */
    @GetMapping("/policy")
    @ResponseBody
    public Object policy(@RequestParam(required = false) String callback) {
        JSONObject result = aliyunOssService.policy();
        if (StringUtils.isBlank(callback)) {
            return result;
        }
        MappingJacksonValue jsonp = new MappingJacksonValue(result);
        jsonp.setJsonpFunction(callback);
        return jsonp;
    }

    /**
     * 上传成功回调方法
     *
     * @param request
     * @return
     */
    @PostMapping("callback")
    @ResponseBody
    public Object callback(HttpServletRequest request) {
        JSONObject data = new JSONObject();
        String filename = request.getParameter("filename");
        filename = "http://".concat(OssConstant.ALIYUN_OSS_BUCKET_NAME).concat(".").concat(OssConstant.ALIYUN_OSS_ENDPOINT).concat("/").concat(filename);
        data.put("filename", filename);
        data.put("size", request.getParameter("size"));
        data.put("mimeType", request.getParameter("mimeType"));
        data.put("width", request.getParameter("width"));
        data.put("height", request.getParameter("height"));
        return new OssResult(OssResultConstant.SUCCESS, data);
    }

    public static void main(String[] args) {
        //进行简单上传测试
        // endpoint以杭州为例，其它region请按实际情况填写
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建
        String accessKeyId = "LTAIK6n9WfBY6v4t";
        String accessKeySecret = "7jaKfgp4fHyLwZBWGtmhueMiLjBMua";
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传字符串
        String content = "Hello OSS";
        ossClient.putObject("shan-oss", "sample-str", new ByteArrayInputStream(content.getBytes()));
        // 关闭client
        ossClient.shutdown();

    }

}
